package cc.miwuya.community.controller;

import cc.miwuya.community.client.UserClient;
import cc.miwuya.community.domain.PropertyItem;
import cc.miwuya.community.domain.RoleUser;
import cc.miwuya.community.service.PropertyItemService;
import cc.miwuya.community.service.RoleMenuService;
import cc.miwuya.community.service.RoleService;
import cc.miwuya.community.service.RoleUserService;
import cc.miwuya.owner.domain.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 迷雾森林
 * @Date 2024-02-29 8:57
 */
@RestController
@RequestMapping("/community/user")
public class UserController {
    @Resource
    RoleService roleService;
    @Resource
    RoleUserService roleUserService;
    @Resource
    RoleMenuService roleMenuService;
    @Resource
    UserClient userClient;
    @Resource
    PropertyItemService propertyItemService;

    /**
     * 根据用户名获取用户角色
     */
    @GetMapping("/getUserRoleByUsername/{username}")
    public List<RoleUser> getUserRoleByUsername(@PathVariable String username) {
        User user = userClient.getUserByUsername(username);
        return roleUserService.list(new QueryWrapper<RoleUser>().eq("user_id", user.getId()));
    }

    /**
     * 根据用户ID获取用户归属物业
     * @param userId
     * @return
     */
    @GetMapping("/getPropertyByUserid/{userId}")
    public Long getPropertyByUserid(@PathVariable Long userId){
        PropertyItem item = propertyItemService.getByUserId(userId);
        System.out.println(userId);
        System.out.println(item);
        if (item==null){
            return (long) -1;
        }
        return item.getId();
    }

}
